#region Description
/*  File Name:      MELoadingScreen.cs
 *  Author:         Adam Goodchild
 *  
 *  Description:    This is a Loading Screen.
 *                  It is used when Content needs to be loaded, such as when a Level or
 *                  GamePlay Screen has to be loaded.
 *  
 *  Sources:        The Idea for this class was gained from the following source(s):
 *                  Microsoft XNA Creators Club - Game State Management Sample
*/
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using MinimizeEngine.Managers;
using MinimizeEngine.Screens;
#endregion

namespace MinimizeEngineDemo.Screens
{
    public class LoadingScreen : MEBaseScreen
    {
        bool mLoadingIsSlow;
        bool mOtherScreensAreGone;

        MEBaseScreen[] mScreensToLoad;

        private LoadingScreen(bool loadingIsSlow, MEBaseScreen[] screensToLoad)
        {
            mLoadingIsSlow = loadingIsSlow;
            mScreensToLoad = screensToLoad;

            TransitionOnTime = TimeSpan.FromSeconds(0.5);
        }

        public static void Load(bool loadingIsSlow, params MEBaseScreen[] screensToLoad)
        {
            foreach (MEBaseScreen screen in MEScreenManager.GetScreens())
            {
                screen.ExitScreen();
            }

            LoadingScreen loadingScreen = new LoadingScreen(loadingIsSlow, screensToLoad);

            MEScreenManager.AddScreen(loadingScreen, 0);
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            if (mOtherScreensAreGone)
            {
                MEScreenManager.RemoveScreen(this);

                foreach (MEBaseScreen screen in mScreensToLoad)
                {
                    if (screen != null)
                    {
                        MEScreenManager.AddScreen(screen, 0);
                    }
                }

                MEEngineManager.Game.ResetElapsedTime();
            }
        }

        public override void Draw(GameTime gameTime)
        {
            if ((ScreenState == ScreenState.Active) &&
                (MEScreenManager.GetScreens().Length == 1))
            {
                mOtherScreensAreGone = true;
            }

            if (mLoadingIsSlow)
            {
                const string message = "This is the Loading Screen...";

                // Center the text in the viewport.
                Viewport viewport = MEEngineManager.DeviceManager.GraphicsDevice.Viewport;
                Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);
                Vector2 textSize = MEScreenManager.Font.MeasureString(message);
                Vector2 textPosition = (viewportSize - textSize) / 2;

                Color color = new Color(255, 255, 255, TransitionAlpha);

                MEScreenManager.SpriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred,
                                                SaveStateMode.SaveState);
                MEScreenManager.SpriteBatch.DrawString(MEScreenManager.Font, message, textPosition, color);
                MEScreenManager.SpriteBatch.End();
            }
        }
    }
}
